﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.Runtime.InteropServices;
using EnvDTE;
using Microsoft.VisualStudio.TextManager.Interop;
using EnvDTE80;

namespace ScaffoldIt
{
    using Constants = Microsoft.VisualStudio.Shell.Interop.Constants;

    static class VsItemsHelpers
    {
        public static IEnumerable<string> GetAllProjectNames(this EnvDTE80.DTE2 DTE)
        {
            foreach (Project prj in DTE.Solution.Projects)
            {
                if (prj.Kind != ProjectKinds.vsProjectKindSolutionFolder)
                    yield return prj.Name;
                else
                {
                    foreach (var item in GetAllProjectNames(prj.ProjectItems))
                    {
                        yield return item;
                    }
                }
            }
        }

        public static IEnumerable<string> GetAllProjectNames(this ProjectItems items)
        {
            if (items == null) yield break;

            foreach (ProjectItem item in items)
            {
                var prj = item.Object as Project;
                if (prj != null && prj.Kind != ProjectKinds.vsProjectKindSolutionFolder)
                {
                    var prjfile = prj.FileName;
                    if (prjfile.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) ||
                        prjfile.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase))
                    yield return prj.Name;
                }
                else
                {
                    foreach (var name in GetAllProjectNames(item.ProjectItems))
                    {
                        yield return name;
                    }
                }
            }
        }

        public static CodeClass CurrentCodeClass(this EnvDTE80.DTE2 DTE, string file, ProjectItem item)
        {

            IVsTextView vTextView = null;
            vTextView = DTE.GetTextViewFor(file);
            if (vTextView == null) return null;

            int lineStart = 0;
            int colStart = 0;
            int lineEnd = 0;
            int colEnd = 0;

            vTextView.GetSelection(out lineStart, out colStart, out lineEnd, out colEnd);

            if (item.FileCodeModel != null)
            {
                var selectedcodeclass = item.FileCodeModel.CodeElements.GetClasses(
                    codeclass =>
                    {
                        return (codeclass.StartPoint.Line <= lineStart && codeclass.EndPoint.Line >= lineEnd);
                    }).FirstOrDefault();

                return selectedcodeclass;
            }

            return null;
        }

        public static string CurrentFile(this EnvDTE80.DTE2 DTE)
        {
            IntPtr hierarchyPtr, selectionContainerPtr;
            uint projectItemId;
            IVsMultiItemSelect mis;

            IVsMonitorSelection monitorSelection = (IVsMonitorSelection)Package.GetGlobalService(typeof(SVsShellMonitorSelection));

            monitorSelection.GetCurrentSelection(out hierarchyPtr, out projectItemId, out mis, out selectionContainerPtr);

            if (hierarchyPtr != IntPtr.Zero)
            {
                IVsHierarchy hierarchy =
                    Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)) as IVsHierarchy;

                if (hierarchy != null)
                {
                    string filename = null;
                    /*
                    hierarchy.GetProperty(projectItemId, (int)__VSHPROPID.VSHPROPID_Name, out value);
                    string filename = this.GetProperty<string>(hierarchy, projectItemId, (int)__VSHPROPID.VSHPROPID_Name);
                    */

                    hierarchy.GetCanonicalName(projectItemId, out filename);

                    return filename;
                }
            }

            return null;
        }
    }
}
